package com.icangya.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.icangya.DAO.*;
import com.icangya.domain.*;
import com.icangya.service.IMissionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;


@Service("MissionServiceImpl")
public class MissionServiceImpl implements IMissionService {

    @Autowired
    private IFbBackMissionDAO iFbBackMissionDAO;

    @Autowired
    private IFbMissionAwardDAO iFbMissionAwardDAO;

    @Autowired
    private IFbMissionRecordDAO iFbMissionRecordDAO;

    @Autowired
    private IFbUserMissionDAO iFbUserMissionDAO;

    @Autowired
    private IFbTurntableRecordDAO iFbTurntableRecordDAO;

    @Autowired
    private IFbUserAwardRecordDAO iFbUserAwardRecordDAO;

    @Autowired
    private IFbAwardDetailsDAO iFbAwardDetailsDAO;

    @Autowired
    private IFbAddressDAO iFbAddressDAO;

    @Autowired
    private IFbOrderDAO iFbOrderDAO;

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    public List<FbBackMission> getFbBackMission() {

        return iFbBackMissionDAO.getFbBackMission();
    }

    @Override
    public List<FbMissionAward> getFbMissionAward() {
        return iFbMissionAwardDAO.getMissionAward();
    }

    @Override
    public FbBackMission getMissionByMissionId(long missionId) {
        return iFbBackMissionDAO.getMissionByMissionId(missionId);
    }

    //获取可领取的奖励
    @Override
    public List<FbMissionAward> getMissionAwardStatus() {
        return iFbMissionAwardDAO.getMissionAwardStatus();
    }

    @Override
    public FbMissionAward getMissionAward(long awardId) {

        return iFbMissionAwardDAO.get(awardId);
    }

    @Override
    public Boolean updateMissionAward(FbMissionAward fbMissionAward) {

        return iFbMissionAwardDAO.update(fbMissionAward);
    }

   // 获取当天的任务信息
    @Override
    public JSONArray getMissionRecordNow( String openId) {

        List<FbMissionRecord> list= iFbMissionRecordDAO.getFbMissionRecord(new Timestamp(new Date().getTime()));
        if(list==null){
            return null;
        }
            JSONArray jsonArray=new JSONArray();
            for (FbMissionRecord missionRecord:list) {
                try {
                    JSONObject object = new JSONObject();
                    object.put("id", missionRecord.getId());
                    object.put("missionName", missionRecord.getMissionName());//任务名
                    object.put("missionContent", missionRecord.getMissionContent());//任务内容
                    object.put("createTime", missionRecord.getCreateTime());
                    object.put("status",0);
                     FbUserMission userMission=  iFbUserMissionDAO.getUserMissByMissIdOrOpenId(openId,missionRecord.getId());
                     if(userMission!=null){
                         object.put("status",1);
                     }
                    jsonArray.add(object);
                }catch(NullPointerException e){
                    continue;
                }
        }
        return jsonArray;
    }

    //下旬当天某个任务的id
    @Override
    public FbMissionRecord getMissionRecord(Timestamp now, long missionId) {
        return iFbMissionRecordDAO.getMissionRecord(now,missionId);
    }

    @Override
    public FbMissionRecord saveFbMissionRecord(FbMissionRecord fbMissionRecord) {
        return iFbMissionRecordDAO.get(iFbMissionRecordDAO.save(fbMissionRecord));
    }

    // 添加用户领取任务的记录
    @Override
    public int saveUserMission(FbUserMission userMission) {

        logger.info("领取任务的id========================"+userMission.getMissionId());
        FbMissionRecord missionRecord= iFbMissionRecordDAO.get(userMission.getMissionId());
        if(missionRecord==null){
            return 1;
        }
        FbUserMission userMiss= iFbUserMissionDAO.getUserMissByMissIdOrOpenId(userMission.getOpenId(),userMission.getMissionId());
        if(userMiss!=null){
            return 2;
        }
        userMission.setMissionName(missionRecord.getMissionName());//任务名
        userMission.setMissionContent(missionRecord.getMissionContent());//任务内容

        FbUserMission fbUserMission=  iFbUserMissionDAO.get(iFbUserMissionDAO.save(userMission));
        if(fbUserMission!=null)
            return 0;
        return 1;

    }

    // 查询用户当天领取后未完成的任务信息
    @Override
    public Object[] getUserMissionIdNow(long missionId) {
        return iFbUserMissionDAO.getUserMissionIdNow(missionId);
    }

    @Override
    public FbUserMission getFbUserMission(long id) {
        return iFbUserMissionDAO.get(id);
    }

    @Override
    public Boolean updateUserMission(FbUserMission userMission) {
        return iFbUserMissionDAO.update(userMission);
    }
// 查询用户未领取的奖励记录
    @Override
    public List<FbTurntableRecord> getFbTurntableRecord(long uid) {
        return iFbTurntableRecordDAO.getFbTurntableRecord(uid);
    }

    @Override
    public FbTurntableRecord saveFbTurntableRecord(FbTurntableRecord turntableRecord) {
        return iFbTurntableRecordDAO.get(iFbTurntableRecordDAO.save(turntableRecord));
    }

    @Override
    public boolean updateTurntableRecord(FbTurntableRecord turntableRecord) {
        return iFbTurntableRecordDAO.update(turntableRecord);
    }

    @Override
    public long getTurntableRecordCount(String openId) {
        return iFbTurntableRecordDAO.getFbTurntableRecordCount(openId);
    }

   //查询系统当天赠送的记录
    @Override
    public FbTurntableRecord getTurntableRecordBySystem(long missionId,String openId) {
        return iFbTurntableRecordDAO.getTurntableRecordBySystem(missionId,openId);
    }

    //添加用户奖励记录
    @Override
    public FbUserAwardRecord saveUserAwardRecord(FbUserAwardRecord userAwardRecord) {
        return iFbUserAwardRecordDAO.get(iFbUserAwardRecordDAO.save(userAwardRecord));
    }

    @Override
    public boolean updateUserAwardRecord(FbUserAwardRecord userAwardRecord) {
        return iFbUserAwardRecordDAO.update(userAwardRecord);
    }

    @Override
    public FbUserAwardRecord getFbUserAwardRecord(long userAwardId) {
        return iFbUserAwardRecordDAO.get(userAwardId);
    }

    @Override
    public FbUserAwardRecord getUserAwardByAwardId(long awardId,String openId) {
        return iFbUserAwardRecordDAO.getUserAwardById(awardId,openId);
    }

    @Override
    public List<FbUserAwardRecord> getUserAwardRecord(String openId) {

        return iFbUserAwardRecordDAO.getUserAwardRecord(openId);
    }

    //根据奖励id查询奖励商品的详情
    @Override
    public FbAwardDetails getAwardDetailsByAwardId(long awardId) {
        return iFbAwardDetailsDAO.getAwardDetailsByAwardId(awardId);
    }

    //添加地址
    @Override
    public FbAddress saveFbAddress(FbAddress address) {
        return iFbAddressDAO.get(iFbAddressDAO.save(address));
    }

    //查询用户所填写的地址
    @Override
    public List<FbAddress> getFbAddressAll(String openId) {

        return iFbAddressDAO.getFbAddressAll(openId);
    }

    @Override
    public FbAddress getAddressByOpenId(String openId, long id) {
        return iFbAddressDAO.getAddressByOpenId(openId,id);
    }

    @Override
    public Boolean updateFbAddress(FbAddress address) {
        return iFbAddressDAO.update(address);
    }
    //获取默认地址
    @Override
    public FbAddress getAddressDefault(String openId) {
        return iFbAddressDAO.getAddressDefault(openId);
    }

    @Override
    public FbOrder saveFbOrder(FbOrder order) {
        return iFbOrderDAO.get(iFbOrderDAO.save(order));
    }

    @Override
    public List<FbOrder> getUserFbOrder(String openId) {
        return iFbOrderDAO.getUserFbOrder(openId);
    }

    @Override
    public List<FbOrder> getFbOrderAll() {
        return iFbOrderDAO.getFbOrderAll();
    }

    @Override
    public List<FbOrder> getFbOrderStatusAll(int status, int paymentStatus) {

        return iFbOrderDAO.getFbOrderStatusAll(status, paymentStatus);
    }
    @Override
    public FbOrder getOrderByOutTrantId(String orderNum) {
        return iFbOrderDAO.getFbOrderByOrderId(orderNum);

    }
    @Override
    public boolean updateOrder(FbOrder fbOrder) {

        return iFbOrderDAO.update(fbOrder);
    }
}
























